 package Game.stonepuzzle.src;

 import javax.swing.*;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.util.Random;

public class MainFrame extends JFrame  implements KeyListener{

    int[][] data = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };

//胜利后的数组形状
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    int row; // 0号元素横坐标位置

    int colum; // 0号元素纵坐标位置

    int count;//统计步数


    //构造方法在创建对象的时候就执行
    public MainFrame(){

        this.addKeyListener(this);

        //初始化数据
        initData();
        //初始化窗体
        intFrame();
        //绘制游戏界面
        paintView();

        //设置窗体可见
        setVisible(true);
    }


    // 初始化数据（打乱手里的二维数组）
    private void initData() {
        // 准备Random对象
        Random r = new Random();

        // 遍历二维数组，获取到每一个元素
        for (int i = 0;i<data.length;i++){
            for (int j = 0;j<data[i].length;j++){
                //data[i][j]和随机索引 所指向的元素进行交换
                //data[randomX][randomY]
                int randomX = r.nextInt(4);
                int randomY = r.nextInt(4);
                int temp = data[i][j];
                data[i][j] = data[randomX][randomY];
                data[randomX][randomY] = temp;
            }
        }
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == 0){
                    row = i;
                    colum = j;

                }
            }
        }
        System.out.println(row);
        System.out.println(colum);

    }

    //此方法用于初始化窗体
    public final   void intFrame(){
        setSize(514,595);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("石头迷阵单机版v1.0");
        setAlwaysOnTop(true);//置顶
        setLocationRelativeTo(null);//设置窗体居中
        //清楚默认布局
        setLayout(null);
    }


    // 此方法用于绘制游戏界面
    public final void paintView(){
        // 移除当前面板中所有组件
        getContentPane().removeAll();

        if (victory()){
            //加载胜利图片资源，添加到窗体中
            JLabel jl = new JLabel(new ImageIcon("D:\\project\\java\\Game\\stonepuzzle\\src\\img\\胜利.jpg"));
            jl.setBounds(124,230,266,88);
            getContentPane().add(jl);
        }

        JButton btn = new JButton("重新游戏");
        btn.setBounds(350,20,100,20);
        getContentPane().add(btn);
        btn.setFocusable(false); // 取消程序焦点
        btn.addActionListener(e -> {
            count = 0;
            initData();
            paintView();
        });


        JLabel scoreLabel = new JLabel("步数为："+count);
        scoreLabel.setBounds(50,20,100,20);
        getContentPane().add(scoreLabel);




        for (int i = 0;i<4;i++){
            for (int j =0;j<4;j++){
                JLabel jl = new JLabel(new ImageIcon("D:\\project\\java\\Game\\stonepuzzle\\src\\img\\"+data[i][j]+".jpg"));
                jl.setBounds(50+100*j,90+100*i,100,100);
                getContentPane().add(jl); //继承父类的方法子类没重写方法省略super
            }
        }
        JLabel background = new JLabel(new ImageIcon("D:\\project\\java\\Game\\stonepuzzle\\src\\img\\background.jpg"));
        background.setBounds(26,30,450,484);
        getContentPane().add(background);
        //刷新操作
        getContentPane().repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        move(keyCode);
        // 每一次移动后刷新页面
        paintView();
    }

    // 此方法用于处理移动业务
    private  void move(int keyCode) {

        //胜利后，游戏将进入不可移动的状态
        if (victory()){
            return;
        }
        if (keyCode == 37 ){

            if (colum==3){ //防止索引越界
                return;
            }
            //空白块和右侧数据做交换
            int temp = data[row][colum] ;
            data[row][colum] = data[row][colum+1];
            data[row][colum+1] = temp;
            colum++;
            count++;

        } else if (keyCode == 38) {
            if (row==3){
                return;
            }
            //空白快和下方数据做交换
            int temp = data[row][colum] ;
            data[row][colum] = data[row+1][colum];
            data[row+1][colum] = temp;
            row++;
            count++;
        } else if (keyCode == 39) {
            //空白快和左侧数据做交换
            if (colum==0){
                return;
            }
            int temp = data[row][colum] ;
            data[row][colum] = data[row][colum-1];
            data[row][colum-1] = temp;
            colum--;
            count++;
        } else if (keyCode == 40) {


            if (row==0){
                return;
            }
            System.out.println("下移动业务");
            int temp = data[row][colum] ;
            data[row][colum] = data[row-1][colum];
            data[row-1][colum] = temp;
            row--;
            count++;
        } else if (keyCode == 90) {
            //触发作弊器
            data = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}

            };


        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }


    //判断游戏是否胜利
    public boolean victory() {

        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;

                }
            }
        }
        return true;
    }
}
